﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
namespace Vehicle.Web.Component
{
    /// <summary>
    /// Copyright (C) M-Tear团队
    /// 文 件 名：PageBase.cs
    /// 版    本：v1.00.0000
    /// 创建标识：2009-08-29    Created by 杨光远 V1.00.0000
    /// 功能说明：程序页面基类 ,所有页面继承该页面
    /// 注意事项：无
    /// 更新记录：
    /// </summary>
    public class PageBase : System.Web.UI.Page
    {
   
        #region ---类属性---
        /// <summary>
        /// 系统SessionID值
        /// </summary>
        public int session_ID
        {

            get
            {
                if (Session["id"] != null)
                {
                    return Common.TextHelper.StringToInt(Session["id"].ToString());
                }
                else
                {
                    return 0;
                }
             
            }
           
        }
        #endregion

        #region ---基本方法---
        /// <summary>
        /// 重载页面显示添加加载与错误事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }
    
        /// <summary>
        /// 页面加载
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(System.EventArgs e)
        {

            if (session_ID == 0)
            {

                Response.Clear();
                Response.Write(ShowMessage("发生错误,请重新登陆"));
                Response.End();
            }
            else
            {
             base.OnLoad(e);
            }
        }
        /// <summary>
        /// 提示错误信息并且返回根目录首页
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public string ShowMessage(string message)
        {
            StringBuilder HtmlMessage = new StringBuilder();
            HtmlMessage.AppendLine("<HTML>");
            HtmlMessage.AppendLine("<Head>");
            HtmlMessage.AppendLine("<Title>错误</Title>");
            HtmlMessage.AppendLine("<script language='javascript'>");
            HtmlMessage.AppendLine("alert('" + message + "');");
            HtmlMessage.AppendLine(("location.href='../../../../'"));
            HtmlMessage.AppendLine("</script>");
            HtmlMessage.AppendLine("</Head>");
            HtmlMessage.AppendLine("</HTML>");
            return HtmlMessage.ToString();
        }

        /// <summary>
        /// 控制显示文字个数
        /// </summary>
        /// <param name="inputString">要截取的文字</param>
        /// <param name="len">留下个数</param>
        /// <returns></returns>
        public static string GetSubString(string inputString, int len)
        {
            ASCIIEncoding ascii = new ASCIIEncoding();
            int tempLen = 0;
            string tempString = "";
            byte[] s = ascii.GetBytes(inputString);
            for (int i = 0; i < s.Length; i++)
            {
                if ((int)s[i] == 63)
                {
                    tempLen += 2;
                }
                else
                {
                    tempLen += 1;
                }

                try
                {
                    tempString += inputString.Substring(i, 1);
                }
                catch
                {
                    break;
                }

                if (tempLen > len)
                    break;
            }
            //如果截过则加上半个省略号
            byte[] mybyte = System.Text.Encoding.Default.GetBytes(inputString);
            if (mybyte.Length > len)
                tempString += "…";

            return tempString;
        }
        #endregion
    }
}